package ch04;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/NewAdderServlet")
public class NewAdderServlet extends HttpServlet {	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String str1 = request.getParameter("num1");
			String str2 = request.getParameter("num2");
			int num1 = Integer.parseInt(str1);
			int num2 = Integer.parseInt(str2);
			int result = num1 + num2;
			
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println("<title>덧셈 프로그램</title>");
			out.printf("%d + %d = %d",num1,num2,result);
		}catch(NumberFormatException e){
			RequestDispatcher dispatcher = request.getRequestDispatcher("data-error");
			dispatcher.forward(request,response);
		}
	}
}
